package cn.sunline.dreamvalue.constant;

import java.util.HashMap;
import java.util.Map;

/**
  * @title: CommonResult
  * @projectName dream-value
  * @description: 通用返回体
  * @author 11292
  * @date 2020/5/7 14:59
*/
public class CommonResult<T> {
    private String status;
    private String statusMsg;
    private T result;
    public CommonResult(){

    }
    protected CommonResult(String status, String statusMsg, T result) {
        this.status = status;
        this.statusMsg = statusMsg;
        this.result = result;
    }

    /**
     * 成功返回结果
     *
     * @param result 获取的数据
     */
    public static <T> CommonResult<T> success(T result) {
        return new CommonResult<T>(ResultStatus.SUCCESS.getStatus(), ResultStatus.SUCCESS.getStatusMsg(), result);
    }

    /**
     * 成功返回结果
     *
     * @param result 获取的数据
     * @param  statusMsg 提示信息
     */
    public static <T> CommonResult<T> success(T result, String statusMsg) {
        return new CommonResult<T>(ResultStatus.SUCCESS.getStatus(), statusMsg, result);
    }


    /**
     * 通用返回体构造
     * @param status
     * @param result
     * @param <T>
     * @return
     */
    public static<T> CommonResult<T> result(ResultStatus status,T result){
        return  new CommonResult<T>(status.getStatus(),status.getStatusMsg(),result);
    }

    /**
     * 通用返回体构造
     * @param status
     * @param <T>
     * @return
     */
    public static<T> CommonResult exceptionResult(ResultStatus status){
        Map<String,String> result = new HashMap<String,String>();
        result.put("errorCode",status.getStatus());
        result.put("errorMsg",status.getStatusMsg());
        return  new CommonResult<Map<String,String>>(ResultStatus.FAILED.getStatus(),ResultStatus.FAILED.getStatusMsg(),result);
    }

    /**
     * 失败返回结果
     * @param statusMsg 提示信息
     */
    public static <T> CommonResult<T> failed(String statusMsg) {
        return new CommonResult<T>(ResultStatus.FAILED.getStatus(), statusMsg, null);
    }

    /**
     * 失败返回结果
     * @param statusMsg 提示信息
     */
    public static <T> CommonResult<T> failed(String status,String statusMsg) {
        return new CommonResult<T>(status, statusMsg, null);
    }



    /**
     * 失败返回结果
     */
    public static <T> CommonResult<T> failed() {
        return failed(ResultStatus.FAILED.getStatusMsg());
    }


    public String getStatus() {
        return status;
    }

    public void setstatus(String status) {
        this.status = status;
    }

    public String getStatusMsg() {
        return statusMsg;
    }

    public void setstatusMsg(String statusMsg) {
        this.statusMsg = statusMsg;
    }

    public T getresult() {
        return result;
    }

    public void setresult(T result) {
        this.result = result;
    }
}
